package com.example.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.foodapp.util.OkHttpUtil;
import com.google.gson.Gson;

import org.json.JSONObject;

import com.example.foodapp.bean.User;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //定义控件
    EditText etLogonPhone, etLogonPassword;
    Button btnLogon;
    RequestQueue queue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //找到控件
        etLogonPassword = findViewById(R.id.et_logon_password);
        etLogonPhone = findViewById(R.id.et_logon_phone);

        btnLogon = findViewById(R.id.btn_logon);
        btnLogon.setOnClickListener(this);
    }


    //跳转到注册页面
    public void register(View view) {
        Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
        startActivity(intent);
    }

    //登陆判断
    @Override
    public void onClick(View v) {
        {
            //找到输入框中的值
            String telephone = etLogonPhone.getText().toString();
            String password = etLogonPassword.getText().toString();

            //登陆判断
            if (telephone.equals("") || password.equals("")) {
                Toast.makeText(MainActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            Map<String, String> map = new HashMap<>();
            map.put("telephone", telephone);
            map.put("password", password);

            queue = Volley.newRequestQueue(this);
            JSONObject jsonRequest = new JSONObject(map);

            JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, "http://192.168.216.207:8080/login", jsonRequest, new Response.Listener<JSONObject>() {
                //处理返回值的方法
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("TAG", "onResponse: " + response);

                    //创建一个Gson(Gson用于将JSON类型的值进行转换)
                    Gson gson = new Gson();
                    //用Gson的fromJson方法进行转换
                    User user = gson.fromJson(response.toString(), User.class);

                    //User实体类中isSuccess()方法获得返回的ture或false
                    if (user.isSuccess()) {
                        Intent intent = new Intent(MainActivity.this, MenuListActivity.class);
                        startActivity(intent);
                        int id = user.getData().get(0).getId();
                        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putInt("userid", id);
                        editor.apply();
                    } else {
//                        Looper.prepare();
                        Toast.makeText(MainActivity.this, "账号或密码输入有误，请重新输入", Toast.LENGTH_SHORT).show();
//                        Looper.loop();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //若没有连接到后台就显示网络错误
                    Toast.makeText(MainActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                }
            });
            queue.add(request);
        }
    }
}